Analyze the sales by location (state) with a bar plot. Since state and city are multiple features (variables), they should be split. Which state has the highes revenue?
# Joining Dataleft_join(orderlines_tbl, bikes_tbl, by =c("product.id"="bike.id"))
# Chaining commands with the pipe and assigning it to order_items_joined_tblbike_orderlines_joined_tbl <- orderlines_tbl %>%left_join(bikes_tbl, by =c("product.id"="bike.id")) %>%left_join(bikeshops_tbl, by =c("customer.id"="bikeshop.id"))# Wrangling Databike_orderlines_joined_tbl %>%select(category) %>%filter(str_detect(category, "^Mountain")) %>%unique()
sales_by_year_and_state_tbl %>%ggplot(aes(x = state, y = sales)) +# Geometriesgeom_col(fill ="#2DC6D6") +# Use geom_col for a bar plotgeom_label(aes(label = sales_text)) +# Adding labels to the barsgeom_smooth(method ="lm", se =FALSE) +# Adding a trendline# Formatting# scale_y_continuous(labels = scales::dollar) + # Change the y-axis. # Again, we have to adjust it for euro valuesscale_y_continuous(labels = scales::dollar_format(big.mark =".", decimal.mark =",", prefix ="", suffix =" €")) +labs(title ="Revenue by state",x ="States",y ="Revenue" ) +theme(axis.text.x =element_text(angle =45, hjust =1))
#> `geom_smooth()` using formula = 'y ~ x'
2 Challenge 2
Question 2:
Analyze the sales by location and year (facet_wrap). Because there are 12 states with bike stores, you should get 12 plots.
Code:
# Get state's sales per yearsales_by_state_in_year <-separate(bike_orderlines_wrangled_tbl,col = location,into =c("city", "state"),sep =", ",convert = T)sales_by_state_in_year
sales_by_state_in_year <- sales_by_state_in_year %>%# Select columnsselect(order_date, total_price, state) %>%# Add year columnmutate(year =year(order_date)) %>%# Grouping by year and summarizing salesgroup_by(state, year) %>%summarize(sales =sum(total_price)) %>%# Add a column that turns the numbers into a currency format # (makes it in the plot optically more appealing)# mutate(sales_text = scales::dollar(sales)) <- Works for dollar valuesmutate(sales_text = scales::dollar(sales, big.mark =".", decimal.mark =",", prefix ="", suffix =" €"))
#> `summarise()` has grouped output by 'state'. You can override using the
#> `.groups` argument.
sales_by_state_in_year
# PlotsalesYearStatePlot <- sales_by_state_in_year %>%# Set up x, y, fillggplot(aes(x = year, y = sales, fill = state)) +# Geometriesgeom_col() +# Run up to here to get a stacked bar plot# Facetfacet_wrap(~ state) +# Formattingscale_y_continuous(labels = scales::dollar_format(big.mark =".", decimal.mark =",", prefix ="", suffix =" €")) +labs(title ="Revenue by year and state",subtitle ="Each state is ordered by years 2015 to 2019",fill ="States:" ) +theme(axis.text.x =element_text(angle =45, hjust =1))